<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ScenicController extends Controller
{


   public function getAdd(){

		return view('admin.scenic.add');
	}


  // ----------------------------------------------------------商品添加
  public function postInsert(Request $request)
	{
		// dd($request->all());

		$id = $request->only(['pid']);
       

        $data = $request->only(['name','description','address','ktime','server','price','oldprice']);
        // $data['pname'] = DB::table('scenic')->where('id','=',$id)->value('name');
        // $data['token'] = str_random(50);

        // dd($data);

        $res =  DB::table('scenic')->insert($data);

        if($res){
            return redirect('/admin/scenic/list')->with('success','用户添加成功');

        }else{
        	 return back()->with('error','用户添加失败');
        }
	 }


// -----------------------------------------------------商品列表
    public function getList(Request $request){
      
		// echo '用户列表页';
		//查询所有用户数据
		// $users = DB::table('scenic')->get();

		//获取分页大小
		$num = $request->input('num',5);
		//判断是否有关键字
		if($request->input('keywords')){
			$scenic = DB::table('scenic')->where('name','like','%'.$request->input('keywords').'%')->paginate($num);
		}else{
			$scenic = DB::table('scenic')->paginate($num);
		}
		//获取当前页面接收到的所有参数
		$data = $request->all();
		// var_dump($data);

		//解析模板
		return view('Admin.scenic.list',['scenic'=>$scenic,'request'=>$data]);
	}

// --------------------------------------------------------商品删除
	public  function getDelete(Request $request){
         // dd($request -> input('id'));
     	$id = $request -> input('id');

     	$res = DB::table('scenic')->where('id',$id)->delete();
     	if($res){
            echo 1;
     	}else{
     		echo 2;
     	}
     }
  
// ----------------------------------------------------商品修改

   public function getEdit($id){
             
      $scenic = DB::table('scenic')->where('id',$id)->first();
      
      return view('admin.scenic.edit',['scenic'=>$scenic]);
    
     }


     public function getUpdate(Request $request){
     	// dd($request -> all());
      $id = $request->only(['id']);

      $data = $request->only(['name','description','address','ktime','server','price','oldprice']);
      $res = DB::table('scenic')->where('id',$id)->update($data);
      // var_dump($res);
       if($res){
            return redirect('/admin/scenic/list')->with('success','用户修改成功');

        }else{
        	 return back()->with('error','用户修改失败');
        }
	 
     }



   









}
